package com.kamistoat.meimeistore.product.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoRequest;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfoResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.Query;
import com.kamistoat.common.utils.R;
import com.kamistoat.meimeistore.product.aliyunVideo.InitObject;
import com.kamistoat.meimeistore.product.dao.VideoDao;
import com.kamistoat.meimeistore.product.entity.VideoEntity;
import com.kamistoat.meimeistore.product.service.VideoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service("videoService")
public class VideoServiceImpl extends ServiceImpl<VideoDao, VideoEntity> implements VideoService {

    /**
     * 从nacos的配置文件中导入字段赋值给本地参数
     */
    @Value("${spring.cloud.alicloud.access-key}")
    private String accessId;
    @Value("${spring.cloud.alicloud.secret-key}")
    private String secret;

    /**
     * 获取数据库视频信息
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 从前端提交的params中获取出关键字key
        String key = (String) params.get("key");
        QueryWrapper<VideoEntity> queryWrapper = new QueryWrapper<VideoEntity>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.eq("vid", key).or().eq("description", key).or().eq("title", key);
            });
        }
        IPage<VideoEntity> page = this.page(
                new Query<VideoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 删除视频。同时从数据库和阿里云/本地删除视频
     *
     * @param asList 视频id列表
     */
    @Override
    public void removeVideoByIds(List<Long> asList) throws ClientException {
        for (Long vid : asList) {
            VideoEntity videoEntity = this.getById(vid);
            if (videoEntity.getStoreType() == 0) {
                // 删除数据库和阿里云上的视频数据
                boolean databaseError = this.removeById(vid);
                DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
                deleteVideoRequest.setVideoIds(videoEntity.getVideoIdCloud());
                DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessId, secret);
                DeleteVideoResponse acsResponse = defaultAcsClient.getAcsResponse(deleteVideoRequest);
            }else{
                // 本地服务器实现
            }
        }
    }

    /**
     * 更新视频信息，同时更新数据库和云上
     *
     * @param videoEntity
     * @return
     */
    @Override
    public R updateVideoInfo(VideoEntity videoEntity) throws ClientException {
        // 先从数据库中查出原始视频信息
        VideoEntity videoSource = this.getById(videoEntity.getVid());

        // 先更新云上
        if(videoSource.getStoreType() == 0){
            UpdateVideoInfoRequest updateVideoInfoRequest = new UpdateVideoInfoRequest();
            // 根据数据库原始信息得到视频在阿里云的云上Id
            updateVideoInfoRequest.setVideoId(videoSource.getVideoIdCloud());
            // 更新云上信息
            updateVideoInfoRequest.setTitle(videoEntity.getTitle());
            updateVideoInfoRequest.setDescription(videoEntity.getDescription());
            DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessId, secret);
            UpdateVideoInfoResponse acsResponse = defaultAcsClient.getAcsResponse(updateVideoInfoRequest);
        }else {
            // 本地服务器实现
        }

        // 然后直接覆盖数据库
        this.updateById(videoEntity);

        return R.ok();
    }
}
